
using System;
using Qdb.Ext;
using Qdb.Foundation;
using Qdb.Internal;
using Qdb.Internal.Replication;
using Qdb.Drs.Inside;

namespace Qdb.Drs.Qdb
{
	/// <exclude></exclude>
	public class QdbReplicationReferenceImpl : ObjectReference, IReplicationReference
		, IQdbReplicationReference
	{
		private object _counterPart;

		private bool _markedForReplicating;

		private bool _markedForDeleting;

		internal QdbReplicationReferenceImpl(IObjectInfo objectInfo)
		{
			ObjectReference yo = (ObjectReference)objectInfo;
			Transaction trans = yo.Transaction();
			VirtualAttributes va = yo.VirtualAttributes(trans);
			if (va != null)
			{
				SetVirtualAttributes((VirtualAttributes)va.ShallowClone(
					));
			}
			else
			{
				// No virtu
				SetVirtualAttributes(new VirtualAttributes());
			}
			object obj = yo.GetObject();
			SetObject(obj);
			Ref_init();
		}

		public QdbReplicationReferenceImpl(object myObject, QdbDatabase db, long longPart
			, long version)
		{
			SetObject(myObject);
			Ref_init();
			var va = new VirtualAttributes();
			va.i_database = db;
			va.i_uuid = longPart;
			va.i_version = version;
			SetVirtualAttributes(va);
		}

		public virtual QdbReplicationReferenceImpl Add(QdbReplicationReferenceImpl
			 newNode)
		{
			return (QdbReplicationReferenceImpl)Hc_add(newNode);
		}

		public virtual QdbReplicationReferenceImpl Find(object obj)
		{
			return (QdbReplicationReferenceImpl)Hc_find(obj);
		}

		public virtual void Traverse(IVisitor4 visitor)
		{
			Hc_traverse(visitor);
		}

		public virtual QdbUUID Uuid()
		{
			QdbDatabase db = SignaturePart();
			if (db == null)
			{
				return null;
			}
			return new QdbUUID(LongPart(), db.GetSignature());
		}

		public virtual long Version()
		{
			return VirtualAttributes().i_version;
		}

		public virtual object Object()
		{
			return GetObject();
		}

		public virtual object Counterpart()
		{
			return _counterPart;
		}

		public virtual void SetCounterpart(object obj)
		{
			_counterPart = obj;
		}

		public virtual void MarkForReplicating()
		{
			_markedForReplicating = true;
		}

		public virtual bool IsMarkedForReplicating()
		{
			return _markedForReplicating;
		}

		public virtual void MarkForDeleting()
		{
			_markedForDeleting = true;
		}

		public virtual bool IsMarkedForDeleting()
		{
			return _markedForDeleting;
		}

		public virtual void MarkCounterpartAsNew()
		{
			throw new NotSupportedException("TODO");
		}

		public virtual bool IsCounterpartNew()
		{
			throw new NotSupportedException("TODO");
		}

		public virtual QdbDatabase SignaturePart()
		{
			return VirtualAttributes().i_database;
		}

		public virtual long LongPart()
		{
			return VirtualAttributes().i_uuid;
		}

		public override VirtualAttributes VirtualAttributes()
		{
			return VirtualAttributes(null);
		}

		public sealed override bool Equals(object o)
		{
			if (this == o)
			{
				return true;
			}
			if (o == null || o.GetType().BaseType != o.GetType().BaseType)
			{
				return false;
			}
			IReplicationReference that = (IReplicationReference)o;
			if (Version() != that.Version())
			{
				return false;
			}
			return Uuid().Equals(that.Uuid());
		}

		public sealed override int GetHashCode()
		{
			int result;
			result = Uuid().GetHashCode();
			result = 29 * result + (int)(Version() ^ ((Version()) >> (32 & 0x1f)));
			return result;
		}
	}
}
